﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using RegistryManager.Components;
using DotNetNuke.UI.Utilities;

namespace RegistryManager
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from TaskManagerModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public abstract partial class Registry_View : RegistryManagerModuleBase, IActionable
    {
        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                // ModuleId property comes out of TaskManagerModuleBase : DotNetNuke.Entities.Modules.PortalModuleBase
                rptWishList.DataSource = WishListController.GetWishLists(/*ModuleId*/);
                rptWishList.DataBind();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void rptWishListOnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lnkSelect = e.Item.FindControl("lnkSelect") as LinkButton;
                var lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;

                //var pnlAdminControls = e.Item.FindControl("pnlAdmin") as Panel;

                var curTask = (WishList)e.Item.DataItem;

                //if (IsEditable && lnkEdit != null && pnlAdminControls != null)
                if (IsEditable && lnkDelete != null && lnkSelect != null /*&& pnlAdminControls != null*/)
                {
                    //pnlAdminControls.Visible = true;
                    lnkSelect.CommandArgument = curTask.WishListID.ToString();
                    lnkSelect.Enabled = lnkSelect.Visible = true;
                    //lnkDelete.CommandArgument = lnkEdit.CommandArgument = curTask.WishListID.ToString();
                    //lnkDelete.Enabled = lnkDelete.Visible = lnkEdit.Enabled = lnkEdit.Visible = true;

                    ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile));

                }
                else
                {
                    //pnlAdminControls.Visible = false;
                }
            }
        }


        public void rptWishListOnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                /// Original Code
                //Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit", "tid=" + e.CommandArgument));

                /// Modified Code 
                Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(string.Empty, string.Empty, "ctl", "Select", "mid=" + ModuleId, "tid=" + e.CommandArgument));
            }

            //if (e.CommandName == "Delete")
            //{
            //    WishListController.DeleteTask(Convert.ToInt32(e.CommandArgument));
            //}

            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        #endregion

        #region Optional Interfaces

        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion
    }
}